///<reference path="../../../../../game/module/base/global/event/UIEvent.ts"/>
/**
 * Created by PengWei on 2018/7/24.
 */
namespace mengdao {
    import UIEvent = game.UIEvent;
    import GComponent = fairygui.GComponent;

    export abstract class BaseMediator extends BaseInjector implements IView {
        protected _view: BaseView<any>;

        public set view(view) {
            this._view = view;
        }

        public get view(): BaseView<any> {
            return this._view;
        }

        public get ui():GComponent{
            return this.view.ui
        }

        public active(): void {
            super.active();
            if (this._view) this._view.active();
            this.onUpdate();
            // this.initViewEvents();
        }

        public close(): void {
            this.removeViewEvents();
            this.onClose();
            this.sleep();
            this.view.close();
            //FIXME 嫁接到game基类里面去
            this.dispatchEventWith(UIEvent.POP_BE_CLOSED, this.view.constructor);
        }

        public initialize(): void {
            super.initialize();
            if(this._view)this._view.initialize();
            this.onInit();
            this.initContextEvents();

            /** Mediator默认事件 */
            this.addContextListener(GameEvent.RESIZE, this.onResizeHandler, this);
        }

        protected onResizeHandler(e: GameEvent): void {
            if (this.view) this.view.onResize();
        }

        /**
         *  首次激活时触发
         */
        abstract onInit();

        /**
         *  每次激活时触发
         */
        abstract onUpdate();

        /**
         *  每次关闭时触发
         */
        abstract onClose();

        /**
         *  删除显示对象事件绑定
         */
        protected removeViewEvents(): void {

        }

        /**
         *  添加与外界的事件绑定
         */
        protected initContextEvents(): void {

        }

        /**
         *  添加显示对象事件绑定
         */
        protected initViewEvents(): void {

        }

        /**
         *  销毁面板，接触绑定
         */
        public dispose(): void {
        }

        __setViewReadyCall(callBack:Function,callObj:any){
            this._view.__setViewReadyCall(()=>{
                this.initViewEvents();
                callBack.apply(callObj);
            }, this);
        }
    }
}